Array.prototype.map()
let array = [1.9, 2.1, 3.5]
const rounded = array.map((x) => Math.round(x))
console.log(rounded) // [2, 2, 4]
Array.prototype.flat()
let array = [0.1, 2, ["345", [{key: 6},[{key: 7},{key: 8}]], 9]]
console.log(array.flat()) // 攤開1層
// [0.1, 2, Array(3)]
// [0.1, 2, ['345', Array(2), 9]]
console.log(array.flat(2)) // 攤開2層
// [0.1, 2, '345', Array(2), 9]
// [0.1, 2, '345', [{key: 6},[{key: 7},{key: 8}]], 9]
console.log(array.flat(Infinity)) // 全部攤平
// [0.1, 2, '345', {key: 6}, {key: 7},{key: 8}, 9]
Array.prototype.flatMap()
// identical to arr.map(...args).flat(),
// but slightly more efficient than calling those two methods separately.
const arr1 = [1, 2, 1];
const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1));
// after map => [1, [2, 2], 1]
// after flat => [1, 2, 2, 1]
console.log(result);
// Expected output: Array [1, 2, 2, 1]
Array.prototype.forEach()
let array = []
array = [0.1, 2, "345", {six: 6}, [{key: 6},{key: 7},{key: 8}], 9]
var types_of_array = []
/* forEach 寫法 */
array.forEach((item) => {
types_of_array.push(typeof(item))
})
console.log(types_of_array)
// > Array ["number", "number", "string", "object", "object", "number"]
/* reference index 寫法
for (let i = 0; i < array.length; i++) {
types_of_array.push(typeof(array[i]))
}
*/
Array.from() / Array.fromAsync()
// 可以將 *async* iterable object (e.g., Set, Map) 或是
// array-like (e.g., 有length 且 可用index reference的) object 轉成 array
Array.from("foo");
// [ "f", "o", "o" ]
const set = new Set(["foo", "bar", "baz", "foo"]);
Array.from(set);
// [ "foo", "bar", "baz" ]
const map = new Map([
[1, 2],
[2, 4],
[4, 8],
]);
Array.from(map);
// [[1, 2], [2, 4], [4, 8]]
const mapper = new Map([
["1", "a"],
["2", "b"],
]);
Array.from(mapper.values());
// ['a', 'b'];
Array.from(mapper.keys());
// ['1', '2'];
Array.isArray()
// 判定變數是否為array
console.log(Array.isArray([1, 3, 5]));
// Expected output: true
console.log(Array.isArray('[]'));
// Expected output: false
console.log(Array.isArray([]))
// Expected output: true
console.log(Array.isArray(new Array(5)));
// Expected output: true
console.log(Array.isArray(new Int16Array([15, 33])));
// Expected output: false
Array.of()
// 用帶入的arguments建立一個新的array
const array = Array.of("1", 2.3, 4, [5, 6])
console.log(array)
// > Array ["1", 2.3, 4, Array [5, 6]]
console.log(Array.of()) // 空的array
// > Array []
Array.prototype.push() / pop()
const array = Array.of("1", 2.3, 4, [5, 6])
let popped = array.pop()
console.log(popped) // [5, 6]
array.push(Array.of(7, 8, 9))
console.log(array) // ["1", 2.3, 4, [7, 8, 9]]
Array.prototype.reduce() / reduceRight()
https://ithelp.ithome.com.tw/articles/10243053
Map 的目的是遍歷所有 item,經過處理之後,回傳同樣長度的陣列
filter 同樣是遍歷所有 item,但是回傳符合條件的 items。
不過 Reduce 的功能就更為強大了,不僅僅有累加的功能,更可以實現 map 和 filter 的功能。
看了這位大大的文,真的好有幫助。我自己的心得是reduce可以幫助我們在同一次迭代中對array做不同的操作。
// array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
const tv_shows = [
{name: "AAA", rating: "0+"},
{name: "BBB", rating: "12+"},
{name: "CCC", rating: "0+"},
{name: "DDD", rating: "6+"},
{name: "EEE", rating: "0+"},
{name: "FFF", rating: "18+"},
{name: "GGG", rating: "15+"}
];
const rating_0 = []
const rating_6 = []
const rating_12 = []
const rating_15 = []
const rating_18 = []
tv_shows.reduce((total, currentValue, currentIndex, arr) => {
if (currentValue.rating === '0+') {
rating_0.push(currentValue)
} else if (currentValue.rating === '6+') {
rating_6.push(currentValue)
} else if (currentValue.rating === '12+') {
rating_12.push(currentValue)
} else if (currentValue.rating === '15+') {
rating_15.push(currentValue)
} else if (currentValue.rating === '18+') {
rating_18.push(currentValue)
}
})